package com.mspbots.api.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.mspbots.api.service.TenantConfigurationService;
import com.mspbots.cw.request.TenantConfigurationDTO;
import com.mspbots.dto.enums.Configuration;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * com.mspbots.api.service.impl.TenantConfigurationServiceImpl
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/6/28
 */
@Deprecated
@Service
public class TenantConfigurationServiceImpl extends BaseGenericService implements TenantConfigurationService {

    @Override
    public TenantConfigurationDTO findById(long id) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/configuration/" + id, null);
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, TenantConfigurationDTO.class);
    }

    @Override
    public List<TenantConfigurationDTO> addBatch(List<TenantConfigurationDTO> dtos) {
        Mono<JsonNode> resultMono = this.apiClient.post("/tenant/configuration/add-batch", dtos);
        JsonNode result = resultMono.block();
        dtos = this.objectMapper.convertValue(result, new TypeReference<List<TenantConfigurationDTO>>() {
        });
        return dtos;
    }

    /**
     * 根据tenant 批量查询
     *
     * @param tenant
     * @return
     */
    @Override
    public List<TenantConfigurationDTO> findAllByTenant(long tenant) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/configuration/tenantId/" + tenant);
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, new TypeReference<List<TenantConfigurationDTO>>() {
        });
    }


    @Override
    public TenantConfigurationDTO findByKeyword(long tenantId, String keyword) {
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", tenantId);
        params.put("keyword", keyword);
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/configuration/keyword/", params);
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, TenantConfigurationDTO.class);
    }

    @Override
    public List<TenantConfigurationDTO> listByKeyword(Map<String, Object> queryParams) {
        Mono<JsonNode> resultMono = this.apiClient.get("tenant/configuration/list", queryParams);
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, new TypeReference<List<TenantConfigurationDTO>>() {
        });
    }

    @Override
    public List<TenantConfigurationDTO> selectListByKeyword(Configuration keyword) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/configuration/keyword/" + keyword.getKeyword());
        JsonNode result = resultMono.block();
        return this.objectMapper.convertValue(result, new TypeReference<List<TenantConfigurationDTO>>() {
        });
    }

}
